package com.yc.cloud.openai.portal.controller;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.yc.cloud.common.basic.api.ResponseResult;
import com.yc.cloud.common.basic.exception.Asserts;
import com.yc.cloud.openai.portal.dto.request.ChatCompletionsRequest;
import com.yc.cloud.openai.portal.service.GPTChatService;

import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import reactor.core.publisher.Flux;
/**
 * GPT/
 */
@RestController
@RequestMapping("chat")
@Api(value = "AI聊天接口", tags = "AI聊天接口")
public class GptChatController {

    private final GPTChatService chatService;

    public GptChatController(GPTChatService chatService) {
        this.chatService = chatService;
    }


    //后续看看能不能合并在一起
    @PostMapping(value = "completions/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    @ApiOperation(value = "机器人对话(流式响应)", notes = "支持文本和音频输入,优先音频输入,用户提问,机器人回答问题,流式响应,一个个字回答")
    public Flux<String> completionByStream(@ModelAttribute ChatCompletionsRequest request, @RequestPart(required = false) @ApiParam(value = "音频文件，可选") MultipartFile file) {
        //音频文件 和 消息文本必须有一个不为空
        if (file == null && StrUtil.isEmpty(request.getMessage())) {
            Asserts.fail("消息内容和音频文件不能同时为空");
        }
        //通过对象将file文件信息传递
        return chatService.completionsByStream(request, file);
    }


    //后续看看能不能合并在一起
    @PostMapping(value = "completions")
    @ApiOperation(value = "机器人对话(一次性响应)", notes = "支持文本和音频输入,优先音频输入,用户提问,机器人回答问题,一次性响应,全部答完")
    public ResponseResult<String> completion(@ModelAttribute ChatCompletionsRequest request, @RequestPart(required = false) @ApiParam(value = "音频文件，可选") MultipartFile file) {
        //音频文件 和 消息文本必须有一个不为空
        if (file == null && StrUtil.isEmpty(request.getMessage())) {
            Asserts.fail("消息内容和音频文件不能同时为空");
        }
        return ResponseResult.success(chatService.completions(request, file));
    }


}
